/**
 * 网络请求
 */
class Http {
	static config;
	static uniApi;
	static api;
	static async request({url,data,method = 'GET',data_status=false}) {
		// #ifdef MP-WEIXIN
		if(method !== 'GET'){
			Http.uniApi.showLoading()
		}
		// #endif
		const token = await Http.uniApi.getStorage('token');
		const token_type = await Http.uniApi.getStorage('token_type');
		const res = await Http.api.promisic(uni.request)({
			url: `${Http.config.requset_url}${url}`,
			data,
			method,
			header: {
				'content-type':'application/json',
				'apikey':Http.config.apikey,
				'Authorization':(token_type.data || '') + ' ' + (token.data || '')
			}
		})
		// #ifdef MP-WEIXIN
		if(method !== 'GET'){
			Http.uniApi.hideLoading()
		}	
		// #endif
		const code = res.statusCode.toString().charAt(0)
		if (code && code == '2' || code == '4'){
			if(res.data){
				if(res.data.code === 1001 || res.data.code === 1002 || res.data.code === 1004){
					Http.uniApi.removeStorage('token');
					Http.uniApi.removeStorage('token_type')
					return Http.uniApi.navigateTo({url:'../../pages/login/login'})
				}if(res.data.code === 1008){
					return Http.uniApi.navigateTo({url:'../../pages/address/address'})
				}else if(res.data.code === 1003){
					return await Http.refreshToken({url:url,data:data,method:method,data_status:data_status})
				}else if(res.data.code === 4444 || res.data.code === 4222){
					Http.uniApi.showToast({title:res.data.msg})
				}else if(res.data.code === 1101){
					Http.uniApi.showToast({title:'网络错误！'})
				}
			}
			if(data_status){
				return res.data
			}else{
				return res.data.data		
			}
		}else{
			Http.uniApi.removeStorage('token');
			Http.uniApi.removeStorage('token_type');
		}
	}
	//刷新token
	static async refreshToken(params){
		const res = await Http.request({url:'auth/refresh',method:'PUT',data_status:true});
		if(res.code === 8888){
			await Http.uniApi.setStorage('token',res.data.token);
			await Http.uniApi.setStorage('token_type',res.data.token_type);
			if(!params.url){
				return
			}
			return await Http.request(params);
		}else{
			await Http.uniApi.removeStorage('token');
			await Http.uniApi.removeStorage('token_type');
			return await Http.request(params);
		}
	}
	//图片上传
	static async upload(count){
		
		const res = await Http.api.promisic(uni.chooseImage)({count: count})
		if(res.errMsg === "chooseImage:fail cancel"){
			return
		}
		for(var i = 0;i<res.tempFilePaths.length;i++){
			return await Http.uploadFile(res.tempFilePaths[i])
		}
	}
	static async uploadFile(tempFilePaths){
		Http.uniApi.showLoading('上传中...')
		const token = await Http.uniApi.getStorage('token');
		const token_type = await Http.uniApi.getStorage('token_type')
		const ret = await Http.api.promisic(uni.uploadFile)({
			url : Http.config.requset_url + 'upload/upload_file',
			filePath: tempFilePaths,
			name: 'file',
			header:{
				'content-type':'application/json',
				'apikey':Http.config.apikey,
				'Authorization':(token_type.data || '') + ' ' + (token.data || '')
			}
		})
		Http.uniApi.hideLoading()
		const code = ret.statusCode.toString().charAt(0)
		if (code && code == '2' || code == '4'){
			if(ret.data){
				ret.data = JSON.parse(ret.data)
				if(ret.data.code === 1001 || ret.data.code === 1002 || ret.data.code === 1004){
					Http.uniApi.removeStorage('token');
					Http.uniApi.removeStorage('token_type');
					return Http.uniApi.navigateTo({url:'../../pages/login/login'});
				}if(ret.data.code === 1008){
					return Http.uniApi.navigateTo({url:'../../pages/address/address'});
				}else if(ret.data.code === 1003){
					await Http.refreshToken({})
					return Http.uploadFile(tempFilePaths)
				}else if(ret.data.code === 4444 || ret.data.code === 4222){
					Http.uniApi.showToast({title:ret.data.msg})
				}else if(ret.data.code === 1101){
					Http.uniApi.showToast({title:'网络错误！'})
				}
			}
			return ret.data.data
		}else{
			Http.uniApi.removeStorage('token');
			Http.uniApi.removeStorage('token_type');
		}
	}
}
export default Http